from PySide6.QtWidgets import QMessageBox

from Devices.Andlink.AndlinkWaterleak.Forms.AndlinkWaterleakWindow_UI import Ui_FormAndlinkWaterleakWindow

from Devices.Andlink.Forms.AndlinkWindow import AndlinkWindow
from Devices.Andlink.AndlinkParser import AndlinkParser
from Devices.Andlink.AndlinkWaterleak.AndlinkWaterleakParser import AndlinkWaterleakParser
from Devices.Andlink.DeviceAndlink import DeviceAndlink
from Devices.Andlink.AndlinkWaterleak.DeviceAndlinkWaterleak import DeviceAndlinkWaterleak

class AndlinkWaterleakWindow(AndlinkWindow):
    def __init__(self, supportDevice, parent = None):
        super().__init__(supportDevice, parent)

        self.__ui = Ui_FormAndlinkWaterleakWindow()
        self.__ui.setupUi(self)

        self.__DeviceQueryInfo = DeviceAndlinkWaterleak.DeviceQueryInfo()
        self.__AndlinkWaterleakParser = AndlinkWaterleakParser()

        self.__ui.tabWidget.setVisible(False)
        self.SetAddtionTab(self.__ui.tabWidget)

        self.__ui.pushButton_Query.clicked.connect(self.event_pushButton_Query_clicked)
        self.__ui.pushButton_StopAlarm.clicked.connect(self.event_pushButton_StopAlarm_clicked)
        self.__ui.pushButton_WriteSN.clicked.connect(self.event_pushButton_WriteSN_clicked)
        self.__ui.pushButton_WriteCMEI.clicked.connect(self.event_pushButton_WriteCMEI_clicked)
        self.__ui.pushButton_SetRstMode.clicked.connect(self.event_pushButton_SetRstMode_clicked)
        self.__ui.pushButton_QuickSleep.clicked.connect(self.event_pushButton_QuickSleep_clicked)
        self.__ui.checkBox_EnableSleep.clicked.connect(self.event_checkBox_EnableSleep_clicked)

    def CmdDateReceived(self, data):
        ret, eventType, dataObj = AndlinkParser.ParseEvent(data)
        if ret:
            if "Inform" == eventType or "ParamChange" == eventType:
                deviceQueryInfo = self.__DeviceQueryInfo
                AndlinkWaterleakParser.ParseData(dataObj, deviceQueryInfo)
                self.__DeviceQueryInfo = deviceQueryInfo
                self.UpdataParams()
            else:
                super().CmdDateReceived(data)

    def event_pushButton_Query_clicked(self):
        self.Post(DeviceAndlink.UrlQuery, self.__AndlinkWaterleakParser.PackEmpty(""))
        
    def event_pushButton_StopAlarm_clicked(self):
        payload = self.__AndlinkWaterleakParser.PackEmpty("Control")
        self.Post(DeviceAndlink.UrlControl, payload)

    def event_pushButton_WriteSN_clicked(self):
        payload = AndlinkWaterleakParser.PackWriteSN(self.__ui.lineEdit_SN.text())
        self.Post(DeviceAndlink.UrlInnerCommand, payload)
        
    def event_pushButton_WriteCMEI_clicked(self):
        payload = AndlinkWaterleakParser.PackWriteCMEI(self.__ui.lineEdit_CMEI.text())
        self.Post(DeviceAndlink.UrlInnerCommand, payload)
        
    def event_pushButton_SetRstMode_clicked(self):
        payload = AndlinkWaterleakParser.PackWriteRstMode(self.__ui.comboBox_RstMode.currentIndex())
        self.Post(DeviceAndlink.UrlInnerCommand, payload)
        
    def event_pushButton_QuickSleep_clicked(self):
        payload = AndlinkWaterleakParser.PackQuickSleep()
        self.Post(DeviceAndlink.UrlInnerCommand, payload)
        
    def event_checkBox_EnableSleep_clicked(self):
        payload = AndlinkWaterleakParser.PackSleepEnable(self.__ui.checkBox_EnableSleep.isChecked())
        self.Post(DeviceAndlink.UrlInnerCommand, payload)

    def CoapFinished(self, url, coapMsg):
        self.__AndlinkWaterleakParser.SetDeviceID(self.GetDeviceFullStatus().deviceId)
        if DeviceAndlink.UrlQuery in url:
            if AndlinkWaterleakParser.ParseQuery(coapMsg, self.__DeviceQueryInfo):
                self.UpdataParams()

                self.ShowMessageBox("设备状态查询", "设备状态查询完成!")
            else:
                self.ShowMessageBox("设备状态查询", "设备状态查询信息解析失败!", QMessageBox.Critical)

            return True

        return False

    def UpdataParams(self):
        if 1 == self.__DeviceQueryInfo.waterLeak:
            self.__ui.label_waterLeak.setText("有报警")
            self.__ui.label_waterLeak.setStyleSheet("font:12pt; color:Red")
        else:
            self.__ui.label_waterLeak.setText("无报警")
            self.__ui.label_waterLeak.setStyleSheet("font:12pt; color:Green")

        if 1 == self.__DeviceQueryInfo.voltageStatus:
            self.__ui.label_voltageStatus.setText("有报警")
            self.__ui.label_voltageStatus.setStyleSheet("font:12pt; color:Red")
        else:
            self.__ui.label_voltageStatus.setText("无报警")
            self.__ui.label_voltageStatus.setStyleSheet("font:12pt; color:Green")

        if 1 == self.__DeviceQueryInfo.protectionStatus:
            self.__ui.label_protectionStatus.setText("已开启")
            self.__ui.label_protectionStatus.setStyleSheet("font:12pt; color:Green")
        else:
            self.__ui.label_protectionStatus.setText("未开启")
            self.__ui.label_protectionStatus.setStyleSheet("font:12pt; color:Red")

        self.__ui.label_voltage.setText("%d(mV)" % (self.__DeviceQueryInfo.voltage))
